package io.zhengsh.vvip.leetcode.findandsort;

import java.util.Objects;

/**
 * 最后一个单词的长度
 */
public class Q58_Length_Of_Last_Word {
    public static void main(String[] args) {
        String s = "luffy is still joyboy";
        Solution solution = new Solution();
        int i = solution.lengthOfLastWord(s);
        System.out.println(i);
    }

    static class Solution {
        public int lengthOfLastWord(String s) {
            if (s == null || Objects.equals("", s)) {
                return 0;
            }
            int lastLen = 0;
            for (int i = s.length() - 1; i >= 0; i--) {
                if (s.charAt(i) != ' ') {
                    lastLen++;
                } else if (s.charAt(i) == ' ' && lastLen > 0) {
                    break;
                }
            }
            return lastLen;
        }

        public int lengthOfLastWord1(String s) {
            if (s == null || Objects.equals("", s)) {
                return 0;
            }
            int lastLen = 0, temp = 0;
            for (int i = 0; i < s.length(); i++) {
                if (s.charAt(i) != ' ') {
                    temp++;
                    lastLen = temp;
                } else {
                    temp = 0;
                }
            }
            return lastLen;
        }
    }
}
